/* 
 * File:   commons.h
 * Author: Administrator
 *
 * Created on 2013?8?16?, ??10:08
 */

#ifndef COMMONS_H
#define	COMMONS_H

#ifdef	__cplusplus
extern "C" {
#endif




#ifdef	__cplusplus
}
#endif

#include "p30f6010A.h"
#include <spi.h>
#include <pwm.h>
#include <ports.h>
#include <stdlib.h>
#include <stdio.h>
#include <timer.h>
#include <time.h>
#include <uart.h>
#include <xlcd.h>
#include <string.h>





//????
#define FCY  8000000				// 8Mhz crystal in 4X PLL mode
#define MILLISEC FCY/8000			// 1 mSec delay constant
#define uSEC FCY/8000/1000

#define BAUDRATE 9600
#define TMR_RATE	FCY/64			// Timer count rate with 1:64 prescaler

//???

#define S1 !PORTDbits.RD8
#define S2 !PORTDbits.RD10
#define S3 !PORTDbits.RD12
#define S4 !PORTDbits.RD9
#define S5 !PORTDbits.RD11
//S6??????MCLR

// output of BNC port
#define Out1 PORTBbits.RB15	// lick (1ms) or come (2ms)
#define Out2 PORTBbits.RB14	// odor A on (1ms) and off (2ms)
#define Out3 PORTBbits.RB13	// odor B on (1ms) and off (2ms)
#define Out4 PORTBbits.RB12	// normal water on (1 ms) or off (2ms), bitter water on (3ms) or off (4ms)
#define Out5 PORTBbits.RB8	         // there are conflicts , maybe with LCD for 5 and 6
#define Out6 PORTBbits.RB9 	//
#define Out7 PORTBbits.RB10	// there are conflicts , maybe with LCD for 5 and 6
#define Out8 PORTBbits.RB11	//


// in new board, there may not be LED to indicate correct or wrong
//#define LED_GREEN   PORTAbits.RA15 //???????? new board definition
//#define LED_RED     PORTAbits.RA14 //???????? new board definition

#define Come 1 // Head fixed, always come

// P4 and P10 are Lick dectors for normal training
#define LickOnPin        PORTAbits.RA9 // mice ??? P4 +12V? ??D8?
// P5~P36, beam breaker, Lick dectors for electrophysiological recordings,
// there are eight beam breakers, for radium arm maze


//LCD
#define RS	LATFbits.LATF8 //1-data?0-command
#define RW	LATFbits.LATF2 //1-Read,0-Write
#define E	LATFbits.LATF3//?????

#define DATA	LATD
#define TRIS_DATA  TRISD

#define  RW_TRIS	TRISFbits.TRISF2
#define  RS_TRIS	TRISFbits.TRISF8
#define  E_TRIS		TRISFbits.TRISF3

// LCD fuction , same as before
#define set_function()	lcd_cmd( 0x38 ) //8bit, 5*8, 1/16DUTY
#define cursor_right()  lcd_cmd( 0x14 )//????
#define cursor_left()   lcd_cmd( 0x10 )//????
#define display_shift() lcd_cmd( 0x1C )//????
#define home_clr()      lcd_cmd( 0x01 ) //???
#define home_it()       lcd_cmd( 0x02 ) //????
#define line_2()        lcd_cmd( 0xC0 ) // (0xC0)??????

#define TRUE 1			// TO TEST: iComeVar == TRUE
#define FALSE 0


//??????????  ????16??, ???????????????????????16?
#define  water_sweet    1    //pwm1L		//same as old definition
#define  odor_A         2    //pwm1H		//same as old definition
#define  odor_B         3    //pwm2l		//same as old definition
#define  water_bitter   4    //pwm2h		//same as old definition
#define  odor_C			5	 //
#define  odor_D			6	 //
#define  odor_E			7	 //
#define  odor_F			8	 //
#define  odor_G			9	 //
#define  odor_H			10	 //

// serial port

//  define status: valve
// every status change must be registered in the send word, otherwise it will be incorrect
// first word is for licking and coming, lick: 0x01; not lick: 0x00
// position pointer cannot be 0x55 and  0xAA?
#define SpLick 	 0 	//
#define SpCome 	 1  	// Head fixed, always come
#define SpLick2	 2   //
#define SpCome2	 3   	// Head fixed, always come
// the second word is for performance, false alarm,
#define SpFalseAlarm		 4	// false alarm
#define SpCorrectRejection		 5	// correct rejection
#define SpMiss		 6	// miss
#define SpHit		 7 	// hit
// the third word is for the first set of valves
#define SpWater_sweet	 8     // 1    //pwm1L		//same as old definition
#define SpOdor_A     9         // #define  2    //pwm1H		//same as old definition
#define SpOdor_B	10    //      3    //pwm2l		//same as old definition
#define SpWater_bitter	 11   // 4    //pwm2h		//same as old definition
// the fourth word is for the second set of vales
#define SpValve5	12
#define SpValve6 	13
#define SpValve7 	14
#define SpValve8 	15  // overall eight valves in the current design, may be more later


// the fifth word is for the output value for
#define SpOut1		16  // PORTBbits.RB8	// lick (1ms) or come (2ms)
#define SpOut2	 	17  // PORTBbits.RB9	// odor A on (1ms) and off (2ms)
#define SpOut3	 	18  // PORTBbits.RB10	// odor B on (1ms) and off (2ms)
#define SpOut4		19 // PORTBbits.RB11	// normal water on (1 ms) or off (2ms), bitter water on (3ms) or off (4ms)
// THERE ARE OTHER out, if needed

//press key, s1s1 through s5s5
#define SpS1S1 	 20
#define SpS1S2   21
#define SpS1S3   22
#define SpS1S4	 23
#define SpS1S5   24
#define SpS2S1 	 25
#define SpS2S2   26
#define SpS2S3   27
#define SpS2S4	 28
#define SpS2S5   29
#define SpS3S1 	 30
#define SpS3S2   31
#define SpS3S3   32
#define SpS3S4	 33
#define SpS3S5   34
#define SpS4S1 	 35
#define SpS4S2   36
#define SpS4S3   37
#define SpS4S4	 38
#define SpS4S5   39
#define SpS5S1 	 40
#define SpS5S2   41
#define SpS5S3   42
#define SpS5S4	 43
#define SpS5S5   44


#define SpStepN         51
#define SpSessN         52
#define SptrialNperSess 49
#define SpSessHit       45
#define SpSessMiss      46
#define SpSessFA	    47
#define SpSessCR        48
#define SpSessCorrRate 	50

#define SpFirOdor       53
#define SpFirOdorL      54
#define SpOdorDelay     55
#define SpSecOdor       56
#define SpSecOdorL      57
#define Sptrialtype     58
#define SpITI           59  // 1 start 0 end
#define SpproLpun       60
#define SpSess          61  // 1 start 0 end
#define SpTrain         62  // 1 start 0 end
#define SpOdor_C        64
#define SpOdor_D        65
#define Splaser         66


void __attribute__((__interrupt__, no_auto_psv))  _T2Interrupt(void);
void __attribute__((__interrupt__, no_auto_psv))  _T1Interrupt(void); // return to main if mouse left port
void __attribute__((__interrupt__, no_auto_psv))  _PWMInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv))  _U2TXInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _U2RXInterrupt(void) ;


void Init_LCD( void );		        // lcd???
void Init_PWM(void);
void Init_SPI(void);
void InitUART2(void);
void InitTMR2(void);
void InitTMR1(void);  // ?lcd.c???
//??/?
void Valve_ON_w_BNC(unsigned int N,unsigned int rate,unsigned int Out);//????rate???N??
void Valve_OFF_w_BNC(unsigned int N,unsigned int Out);
void Valve_ON(unsigned int N,unsigned int rate);//????rate???N??
void Valve_OFF(unsigned int N);


void lcd_cmd( char cmd );	        // ????lcd
void lcd_data( char data );		    // ????lcd
void puts_lcd ( unsigned char *data, unsigned char count );//?????
void Mode_Show(unsigned int i);
void LCD_set_xy(unsigned int x,unsigned int y ); //?lcd.c???

// sound effect
//void Music_play(unsigned int i);	//old definition
void beep(void);

void SetupPorts(void);  //????

void Digt_out(unsigned int Nchan, unsigned int Duration);	// digital output in 1
void NewDigt_out(unsigned int Nchan, float Duration);


void counts_dispose(unsigned int counter);//????1000???,??????????????

void DelayNmSec(unsigned int N);//ms     ????
//void DelayNSec(unsigned int N);//s
void DelayNuSec(unsigned int N);//us
void DelayNmSecNew(unsigned int N); //ms HouHan
// serial port
void UART2PutChar(unsigned char Ch);
void Puts_UART2( unsigned char *data, unsigned char count );
void localSendTwice(int thisSp);
void localSendOnce(int thisSp, int thisValue);
void ErrorUartSend(void);
void BNCController(int Channel,int State);

extern char UARTFLag;
extern unsigned int ones,tens,hunds,thous,ten_thous,hund_thous,thous_thous;   //??????????????
extern unsigned int period;//pwm
extern unsigned int timerCounterI,timerCounterJ; //????1ms??????
extern unsigned int U2RX_Buf;
extern int LickHistory;
extern char UartSend[4];


#endif	/* COMMONS_H */

